package com.mlx.mbg.code.method;

import com.mlx.mbg.code.MbgUtil;
import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@EnableMethod
public class DeleteByPrimaryKeyMethod  implements IExtensionMethod {
    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        return getExtensionLevelSet().contains(level) && MbgUtil.checkPrimaryKey(introspectedTable, "deleteByPrimaryKey");
    }

    @Override
    public int getOrder() {
        return 600;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        List<IntrospectedColumn> keyColumnList = introspectedTable.getPrimaryKeyColumns();

        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        FullyQualifiedJavaType methodReturnType = new FullyQualifiedJavaType("int");

        String methodName = "deleteByPrimaryKey";

        List<Parameter> methodParameterList = new ArrayList<Parameter>();
        for (IntrospectedColumn column : keyColumnList){

            String javaProperty = column.getJavaProperty();

            FullyQualifiedJavaType javaType = column.getFullyQualifiedJavaType();

            // 参数类型
            FullyQualifiedJavaType methodParameterType = new FullyQualifiedJavaType(javaType.getShortName());
            // 方法参数
            Parameter methodParameter = new Parameter(methodParameterType, javaProperty);
            methodParameterList.add(methodParameter);

        }

        Method method = new Method();
        method.setVisibility(methodVisibility);
        method.setReturnType(methodReturnType);
        method.setName(methodName);
        for (Parameter parameter : methodParameterList){
            method.addParameter(parameter);
        }
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 主键删除");
        method.addJavaDocLine(" */");

        return Collections.singletonList(method);
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        List<IntrospectedColumn> keyColumnList = introspectedTable.getPrimaryKeyColumns();

        XmlElement deleteByPrimaryKey = new XmlElement("delete");
        deleteByPrimaryKey.addAttribute(new Attribute("id", "deleteByPrimaryKey"));

        StringBuilder stringBuilder = new StringBuilder("where ");
        int size = keyColumnList.size();
        int maxIndex = size - 1;
        for (int i = 0; i < size ; i++){
            IntrospectedColumn column = keyColumnList.get(0);
            String actualColumnName = column.getActualColumnName();
            String javaProperty = column.getJavaProperty();
            String jdbcTypeName = column.getJdbcTypeName();

            if (i != maxIndex){
                stringBuilder.append(actualColumnName).append(" = ").append("#{").append(javaProperty).append(",jdbcType=").append(jdbcTypeName).append("} and ");
            }else {
                stringBuilder.append(actualColumnName).append(" = ").append("#{").append(javaProperty).append(",jdbcType=").append(jdbcTypeName).append("}");
            }
        }

        deleteByPrimaryKey.addElement(new TextElement("delete from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
        deleteByPrimaryKey.addElement(new TextElement(stringBuilder.toString()));

        return Collections.singletonList(deleteByPrimaryKey);
    }
}
